<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字</title>
</head>
<body>
    <button id="resetBtn">重新开始一局游戏</button><br>
    <span>要猜的数字:</span>
    <input type="text">
    <button id="guessBtn">猜</button><br>
    <span>结果:</span><span id="result"></span><br>
    <span>已经猜的次数:</span><span id="guessCount">0</span>
    <script>
        //1先拿到所有的元素
        let resetBtn = document.querySelector('#resetBtn');
        let input = document.querySelector('input');//输入的内容
        let guessBtn = document.querySelector("#guessBtn");
        let result = document.querySelector("#result");
        let guessCount = document.querySelector("#guessCount");
        //2.生成一个随机数 random是获得一个0-1的小数,然后乘100就是[0-100)的数字,floor是向下取整 +1就是1-100内的数字
        let toGuess = Math.floor(Math.random() * 100) + 1;
        console.log(toGuess);
        //实现猜的点击事件
        guessBtn.onclick = function(){
            if(input.value ==''){
                return;
            }

            //获得猜的数字内容
            let num = parseInt(input.value);
            if(num > toGuess){
                //猜高了
                result.innerHTML = '高了';
                result.style.color = 'red';
            }else if(num < toGuess){
                //猜低了
                result.innerHTML = '低了';
                result.style.color = 'red';
            }else{
                result.innerHTML = '对了';
                result.style.color = 'green';
            }
            //4.让猜的次数增加
            let count = parseInt(guessCount.innerHTML);
            guessCount.innerHTML = count + 1;
        }
        //重新开始一局
        resetBtn.onclick = function(){
            //让页面刷新即可  location和document是并列关系的内容 前者是空值页面的连接和地址reload就是刷新了  后者是控制元素的
            location.reload();
        }
    </script>
</body>
</html>